package com.itheima.clientAndService01;

public class SortClass01 {
    public static void main(String[] args) {
        //在数组{1,2,3,4,5,6,7,8,9,10}中,查找某个元素的位置
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        int index = searchMethod(arr, 9);
        System.out.println(index);
    }

    private static int searchMethod(int[] arr, int a) {
        int index = -1;
        int left = 0;
        int right = arr.length - 1;
        while (left < right) {
            int mid = (left + right) / 2;
            if (arr[mid] > a && left <= right) {
                right = mid - 1;
            } else if (arr[mid] < a && left <= right) {
                left = mid + 1;
            } else {
                index = mid;
                break;
            }
        }
        return index;
    }
}
